package com.nokia.pc3.core.xform;

import java.util.Stack;

import com.nokia.pc3.core.format.input.RpmDatum;

/**
 * Parent class of all transformers.
 * 
 * @author jcroke
 */
public abstract class Transformer implements Functional {

	private Transform factoryContainer;
	private Xformer xf;
	private String[] labels;
	private String[] inputv;
	private String[] argv;

	/**
	 * Explicit constructor.
	 */
	public Transformer() {
	};

	/**
	 * Creates a new <code>Transformer</code> instance.
	 * 
	 * @param umem
	 *            MR in memory configuration and transformer storage object.
	 */
	protected void init(Transform tf, Xformer xf) {
		this.factoryContainer = tf;
		this.xf = xf;
		String labelStr = xf.getLabels();
		if (null != labelStr) {
			labelStr.replaceAll("\\s", "");
			this.labels = labelStr.split(",", -1);
		} else {
			this.labels = null;
		}

		String args = xf.getArgs();
		String argsDelim = xf.getArgsDelim();

		// Break out the arguments using args-delimiter (if any)
		// default will be comma if not specified.
		if (null != argsDelim & null != args) {
			this.argv = args.split(argsDelim, -1);
		} else if (null != args) {
			this.argv = args.split(",", -1);
		} else {
			this.argv = null;
		}

		// Break out inputs. Delimiter is a comma.
		String inputs = xf.getInput();
		if (null != inputs) {
			inputs.replaceAll("\\s", "");
			this.inputv = inputs.split(",", -1);
		}
	}

	/**
	 * Sets transformer input of <code>NokiaUmem</code> instance. Used if input
	 * is needed by transformer, but no previous transformers have run.
	 * 
	 * @see InputFileFormat
	 * @param source
	 *            Object reference of source input.
	 */
	public void setInput(Object source) {
		factoryContainer.getTransformUmem().put(xf.getInput(), source);
	}

	/**
	 * Stores transformer output Object to NokiaUmem as defined in DAC.
	 * 
	 * @param output
	 *            Results of transformer.
	 */
	protected void putOutput(Object output) {
		factoryContainer.getTransformUmem().put(xf.getOutput(), output);
	}

	/**
	 * Stores transformer sub output Object to NokiaUmem as defined in DAC. A
	 * sub output location is the normal input location suffixed with additional
	 * location data to allow transformers to have multiple outputs.
	 * 
	 * @param output
	 *            Results of transformer.
	 */
	protected void putOutput(String sub, Object output) {
		factoryContainer.getTransformUmem().put(xf.getOutput() + "." + sub,
				output);
	}
	
	/**
	 * Returns NokiaUmem RPM stack.
	 * 
	 * @return RPM Stack.
	 */
	protected Stack<RpmDatum> getRpmStk() {
		return factoryContainer.getTransformUmem().getRpmStk();
	}

	/**
	 * Returns transformer array of input objects.
	 * 
	 * @return Input object array
	 */
	protected String[] getInputv() {
		return inputv;
	}
	
	/**
	 * Returns transformer first input object of input vector.
	 * 
	 * @return Input object
	 */
	protected String getInput() {
		return inputv[0];
	}

	protected String[] getLabels() {
		return labels;
	}

	protected String[] getArgv() {
		return argv;
	}
	
	protected String getType() {
		// not yet implemented
		return "";
	}
}
